<template>
    <div class="xtx-goods-page">
        <div class="container" v-if="goods">
            <!-- 面包屑 -->
            <el-breadcrumb separator-class="el-icon-arrow-right" style="padding:25px 10px; ">
                <el-breadcrumb-item to="/">首页</el-breadcrumb-item>
                <el-breadcrumb-item :to="'/category/'+goods.categories[0].id">{{goods.categories[0].name}}
                </el-breadcrumb-item>
                <el-breadcrumb-item :to="'/category/sub/'+goods.categories[1].id">{{goods.categories[1].name}}
                </el-breadcrumb-item>
                <el-breadcrumb-item to="/">{{goods.name}}</el-breadcrumb-item>
            </el-breadcrumb>
            <!-- 商品信息 -->
            <div class="goods-info">
                <div class="media">
                    <GoodsImage :images="goods.mainPictures"/>
                    <GoodsSales/>
                </div>
                <div class="spec">
                    <GoodName :goods="goods"/>
                    <GoodsSku :goods-id="goods.id" @change="changeSku"/>
                    <div>
                        <span style="color: #999;margin-left: 10px;margin-right: 20px">数量</span>
                        <el-input-number v-model="num" :min="1" :max="100" size="small"
                                         style="border: 1px solid #ccc;border-radius: 4px"/>
                    </div>
                    <el-button @click="insertCart" type="primary" style="margin-top: 20px;width: 200px">加入购物车</el-button>
                </div>
            </div>
            <!-- 商品推荐 -->
            <GoodsRelevant/>
            <!-- 商品详情 -->
            <div class="goods-footer">
                <div class="goods-article">
                    <!-- 商品+评价 -->
                    <el-tabs v-model="activeName" style="background: white;padding-top: 20px">
                        <el-tab-pane label="商品详情" name="first">
                            <GoodsDetail :goods="goods"/>
                        </el-tab-pane>
                        <el-tab-pane label="商品评价" name="second">
                            <GoodsComment :goods="goods"/>
                        </el-tab-pane>
                    </el-tabs>
                    <!-- 注意事项 -->
                    <GoodsWarn/>
                </div>
                <!-- 24热榜+专题推荐 -->
                <div class="goods-aside">
                    <GoodsHot :type="1"/>
                    <GoodsHot :type="2"/>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
    import GoodsRelevant from "@/views/goods/components/goods-relevant";
    import {findGoods} from "@/api/product";
    import GoodsImage from "@/views/goods/components/goods-image";
    import GoodsSales from "@/views/goods/components/goods-sales";
    import GoodName from "@/views/goods/components/goods-name";
    import GoodsSku from "@/views/goods/components/goods-sku";
    import GoodsDetail from "@/views/goods/components/goods-detail";
    import GoodsComment from "@/views/goods/components/goods-comment";
    import GoodsHot from "@/views/goods/components/goods-hot";
    import GoodsWarn from "@/views/goods/components/goods-warn";

    export default {
        name: "XtxGoodsPage",
        components: {
            GoodsWarn,
            GoodsHot, GoodsComment, GoodsDetail, GoodsSku, GoodName, GoodsSales, GoodsImage, GoodsRelevant
        },
        data() {
            return {
                goods: null,
                currSku: null,
                num: 1,
                activeName: 'second'
            }
        },
        created() {
        },
        mounted() {

        },
        methods: {
            // 获取商品详情
            async findGoods() {
                const {result} = await findGoods(this.$route.params.id)
                this.goods = result
            },
            changeSku(sku) {
                // 修改商品的现价原价库存信息
                if (sku.skuId) {
                    this.goods.price = sku.price
                    this.goods.oldPrice = sku.oldPrice
                    this.goods.inventory = sku.inventory
                    this.currSku = sku
                } else {
                    this.currSku = null
                }

            },
            insertCart() {
                if (!this.currSku) {
                    this.$message.error('请选择商品规格')
                    return;
                }

                if (this.num > this.goods.inventory) {
                    this.$message.error('库存不足')
                    return;
                }
                this.$store.dispatch('cart/insertCart', {
                    id: this.goods.id,
                    skuId: this.currSku.skuId,
                    name: this.goods.name,
                    picture: this.goods.mainPictures[0],
                    price: this.currSku.price,
                    nowPrice: this.currSku.price,
                    count: this.num,
                    attrsText: this.currSku.specsText,
                    selected: true,
                    isEffective: true,
                    stock: this.currSku.inventory
                })
                this.$message.success('加入购物车成功')
            }
        },
        watch: {
            '$route.params.id': {
                handler(newVal) {
                    if (newVal && this.$route.path === ('/product/' + newVal)) {
                        this.findGoods()
                    }
                },
                immediate: true
            }
        }
    }
</script>

<style scoped lang="less">

    .xtx-goods-page {
        background: #f5f5f5;
    }

    .goods-info {
        min-height: 600px;
        background: #fff;
        display: flex;

        .media {
            width: 580px;
            height: 600px;
            padding: 30px 50px;
        }

        .spec {
            flex: 1;
            padding: 30px 30px 30px 0;
        }
    }

    .goods-footer {
        display: flex;
        margin-top: 20px;

        .goods-article {
            width: 940px;
            margin-right: 20px;
        }

        .goods-aside {
            width: 280px;
            min-height: 1000px;
        }
    }

</style>